/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	spi_flash.h
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-06-21
**	Function List： 
**  History:         
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	防止重复包含
*********************************************************************************************************
*/

#ifndef _SPI_FLASH_H_
#define _SPI_FLASH_H_
/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/
#ifdef RTOS
#define	SPI_FLASH_SEM	1
#define SPI_FLASH_ASYN		1
#else
#endif
/*
*********************************************************************************************************
*                                              	头文件
*********************************************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef	RTOS
#include <rtthread.h>
#endif
#include "stm32h7xx_hal.h"
#include "spi.h"

/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/
#define  W25X_SECTOR_SIZE				4096
#define  W25X_PAGE_SIZE					256
#define	 W25X_SEM_WAITE_TICK		0							
#define  W25X_TOTAL_SIZE				0x1000000
#define  W25X_SECTOR_OFFSET			0x1000
#define  W25X_SECTOR_MSK				0xFFFFF000

#define SPI_FLASH_ID						0xEF4018
/* Private define ------------------------------------------------------------*/
#define W25X_WriteEnable				0x06
#define W25X_WriteDisable				0x04
#define W25X_ReadStatusReg			0x05
#define W25X_WriteStatusReg			0x01

#define W25X_Suspend						0x75
#define W25X_Resume							0x7A

#define W25X_PowerDown					0xB9
#define W25X_ReleasePowerDown		0xAB
#define W25X_DeviceID						0xAB
#define W25X_ManufactDeviceID		0x90
#define W25X_JedecDeviceID			0x9F

#define W25X_GlobalBlockLock		0x7E
#define W25X_GlobalBlockUnLock	0x98
#define W25X_EnableReset				0x66
#define W25X_ResetDevice				0x99

#define W25X_ReadUniqueID				0x4B

#define W25X_ChipErase					0xC7
#define W25X_PageProgram				0x02
#define W25X_QuadPageProgram		0x32
#define W25X_SectorErase				0x20
#define W25X_BlockErase					0xD8
#define W25X_ReadData						0x03
#define W25X_FastReadData				0x0B
#define W25X_ReadSFDPRegister		0x5A

#define Dummy_Byte							0xFF

#define __W25Q_CS_SELECT(__FLASH_HANDLE__)		( (__FLASH_HANDLE__)->cs_pin_base->BSRRH = __FLASH_HANDLE__->cs_pin );


#define __W25Q_CS_DESELECT(__FLASH_HANDLE__)	( (__FLASH_HANDLE__)->cs_pin_base->BSRRL = __FLASH_HANDLE__->cs_pin )
/*
*********************************************************************************************************
*                                              枚举和结构体类型定义
*********************************************************************************************************
*/
#define FLASH_SEM_LOCKED		1
#define FLASH_SEM_UNLOCKED	0

#define	FLASH_CS_SELECT		0
#define	FLASH_CS_DESELECT	1

#define FLASH_WRITE_THROUGH	0
#define FLASH_WRITE_ERASE		1
#define FLASH_WRITE_NORMAL	2

typedef enum{
	FLASH_ERR_NONE,
	FLASH_ERR_OPEN,
	FLASH_ERR_STATE,
	FLASH_ERR_LOCK,
	FLASH_ERR_ERASE,
	FLASH_ERR_WRITE,
	FLASH_ERR_READ,
	FLASH_ERR_ALIGN,
}flash_err_t;

typedef enum{
	FLAHS_UNINIT,
	FLASH_IDLE,
	FLASH_OCCUPIED,
	FLASH_ERR,
}flash_state_t;

typedef struct W25Q128{
	SPI_TypeDef*   spi_chn;
	struct SPI_HANDLE * spi_handle;
	GPIO_TypeDef * cs_pin_base;
	uint32_t			 cs_pin;
	uint8_t				 status;
	uint8_t				 errcode;
#ifdef RTOS
	struct rt_semaphore w25q_sem;	/*spi flash 操作互斥量*/
	struct rt_semaphore dma_sem;	/*dma传输互斥量 在dma完成后release*/
#endif
}w25q128_t;
/*
*********************************************************************************************************
*                                              	全局变量声明
*********************************************************************************************************
*/
extern w25q128_t w25q128_handle;

/*
*********************************************************************************************************
*                                              	函数声明
*********************************************************************************************************
*/
w25q128_t* flash_open(void);

flash_err_t flash_chip_erase(w25q128_t*);

flash_err_t flash_read(w25q128_t*,uint32_t addr,uint8_t *buf,uint16_t size);

flash_err_t flash_write(w25q128_t*,uint32_t addr,uint8_t *buf,uint16_t size,uint8_t write_method);

flash_err_t flash_erase(w25q128_t*,uint32_t addr,uint32_t size);

//flash_err_t flash_read_asyn(w25q128_t*,uint32_t addr,uint8_t* buf,uint16_t size);
//flash_err_t flash_write_asyn(w25q128_t*,uint32_t addr,uint8_t* buf,uint16_t size);
//flash_err_t flash_erase_asyn(w25q128_t*,uint32_t addr,uint16_t size);
//flash_err_t flash_wait_asyn();

#endif
